define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'entry/article/index',
                    add_url: 'entry/article/add',
                    edit_url: 'entry/article/edit',
                    del_url: 'entry/article/del',
                    multi_url: 'entry/article/multi',
                    table: 'entry_article',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'hits', title: __('Hits')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(function () {
                classify($(".classify2"), $("input[name='classify1']:checked").val(), 2)
                classify($(".classify3"), $("input[name='classify2']:checked").val(), 3);
            })
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $(".classify1").change(function () {
                    classify($(".classify2"), $("input[name='classify1']:checked").val(), 2);
                })
                $(".classify2").change(function () {
                    classify($(".classify3"), $("input[name='classify2']:checked").val(), 3);
                })

            }
        }
    };
    return Controller;
});
var step = 5;
function classify(obj, pid, level) {
    $.ajax({
        url: "/admin/ajax/classify",
        type: "GET",
        async:false,
        data: {pid:pid},
        success: function (res) {
            if (res.code == '1') {
                var data = res.data;
                var html = '';
                $(obj).find('.radio').html('');
                var j = 0;
                var checkd = '';
                var name = 'classify'+level;
                if (level == 3) {name = 'row[classify_id]';}
                for (var i in res.data) {
                    checkd = j == 0 ? 'checked' : '';
                    html += ' <label for="classify'+step+'">\n' +
                        '<input id="classify'+step+'" name="'+name+'" '+checkd+' type="radio" value="'+i+'"> '+data[i]+'\n' +
                        '</label>';
                    step++;
                    j++;
                }
                $(obj).find('.radio').append(html);
            }
        },
        error: function (err) {}
    });
}
